<?php

namespace zhtfrontend\models\datas;

use common\components\helpers\Fun;
use Yii;
use yii\helpers\ArrayHelper;
use yii\db\ActiveRecord;
use yii\behaviors\TimestampBehavior;
use common\models\datas\Customer;
use common\components\extenders\MenuManage;


/**
 * This is the model class for table "{{%comment}}".
 *
 * @property integer $id
 * @property integer $title
 * @property string $content
 * @property integer $created_at
 * @property integer $site_id
 * @property integer $state
 * @property integer $user_id
 * @property integer $check_time
 * @property integer $valuator_id
 * @property string $valuator_type
 * @property integer $accepter_id
 * @property string $accepter_type
 */
class Comment extends \zhtfrontend\components\bases\BaseModel
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%comment}}';
    }

    /**
     * @return \yii\db\Connection the database connection used by this AR class.
     */

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
//            [['id','customer_id','object_type','teacher_id'], 'required'],
//            [['object_type'], 'required'],
            [['id', 'object_type', 'object_id', 'state', 'user_id'], 'integer'],
            [['title', 'description'], 'string']
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('zhtfrontend', 'ID'),
            'title' => Yii::t('zhtfrontend', '标题'),
            'description' => Yii::t('zhtfrontend', '评论描述'),
            'created_at' => Yii::t('zhtfrontend', '创建时间'),
            'site_id' => Yii::t('zhtfrontend', '分店编号'),
            'state' => Yii::t('zhtfrontend', '状态（0未审核，1正常显示）'),
            'user_id' => Yii::t('zhtfrontend', '审核管理员'),
            'check_time' => Yii::t('zhtfrontend', '审核时间'),
            'valuator_id' => Yii::t('zhtfrontend', '评价者'),
            'valuator_type' => Yii::t('zhtfrontend', '评价者类型'),
            'accepter_id' => Yii::t('zhtfrontend', '被评价者'),
            'accepter_type' => Yii::t('zhtfrontend', '被评价者类型'),
        ];
    }

    public function fields()
    {
        $fields = parent::fields();
        $fields['state'] = function () {
            switch ($this->state) {
                case 1:
                    return \Yii::t('zhtfrontend', '显示');
                case 0:
                    return \Yii::t('zhtfrontend', '屏蔽');
            }
        };
        $fields['created_at'] = function () {
            return \Yii::$app->formatter->asDatetime($this->created_at);
        };

        $fields['valuator_type_name'] = function () {
            switch ($this->valuator_type) {
                case 1:
                    //查询管理员表，查看该评价人角色
                    $admin = Admin::findOne(['id'=>$this->valuator_id]);
                    switch ($admin->role) {
                        case 'manager':
                            return \Yii::t('zhtfrontend', '工作人员');
                        case 'teache_teacher':
                            return \Yii::t('zhtfrontend', '老师');
                        case 'class_teacher':
                            return \Yii::t('zhtfrontend', '班主任');
                        case 'mechanic':
                            return \Yii::t('zhtfrontend', '机构');
                    }
                case 2:
                    return \Yii::t('zhtfrontend', '学生或家长');
            }
        };
        $fields['valuator_name'] = function () {
            if($this->valuator_type == 1){
                $admin  = Admin::findOne(['id'=>$this->valuator_id]);
                return \Yii::t('zhtfrontend', $admin->username);
            }else{
                $customer = Customer::findOne(['id'=>$this->valuator_id]);
                return \Yii::t('zhtfrontend', $customer->name);
            }
        };
        $fields['accepter_type_name'] = function () {
            switch ($this->accepter_type) {
                case 1:
                    //查询管理员表，查看该评价人角色
                    $admin = Admin::findOne(['id'=>$this->accepter_id]);
                    switch ($admin->role) {
                        case 'manager':
                            return \Yii::t('zhtfrontend', '工作人员');
                        case 'teache_teacher':
                            return \Yii::t('zhtfrontend', '老师');
                        case 'class_teacher':
                            return \Yii::t('zhtfrontend', '班主任');
                        case 'mechanic':
                            return \Yii::t('zhtfrontend', '机构');
                    }
                case 2:
                    return \Yii::t('zhtfrontend', '学生或家长');
            }
        };
        $fields['accepter_name'] = function () {
            if($this->accepter_type == 1){
                $admin  = Admin::findOne(['id'=>$this->accepter_id]);
                return \Yii::t('zhtfrontend', $admin->username);
            }else{
                $customer = Customer::findOne(['id'=>$this->accepter_id]);
                $customer_name = !empty($customer)?$customer->name:'--';
                return \Yii::t('zhtfrontend', $customer_name);
            }
        };

        return $fields;
    }

    public function extraFields()
    {
        return [
            'operate' => function () {
                return (new MenuManage(MenuManage::RECORD_ITEM_TOOLBAR, $this->attributes))->create();
            },
        ];

    }

    public function getCommentRelations()
    {
        return $this->hasMany(CommentRelation::className(), ['comment_id' => 'id']);
    }

    public function getTeacher()
    {
        if($this->accepter_type == 3){
            return $this->hasOne(Teacher::className(), ['id' => 'accepter_id']);
        }
    }

    public function getInfos()
    {
        return $this->hasMany(CommentInfo::className(), ['id' => 'comment_info_id'])
            ->via('commentRelations');
    }

    public function behaviors()
    {
        return ArrayHelper::merge(parent::behaviors(), [
            [
                'class' => TimestampBehavior::className(),
                'attributes' => [
                    ActiveRecord::EVENT_BEFORE_INSERT => ['created_at'],
                ],
            ],

        ]);


    }
}
